import java.util.Arrays;

public class ArrayList {
    //常量
    public static final int DEFAULT_SIZE = 10;
    //下标的有效值
    public int useSize;
    //初始化一个数组
    public int[] arr = new int[DEFAULT_SIZE];

    public void myArratList() {
        this.arr = new int[DEFAULT_SIZE];
    }
    public void disPlay() {
        //遍历顺序表
        for (int i = 0; i < useSize; i++) {
            System.out.println(arr[i]);
        }
    }
    //新添元素
    public void add(int date) {
        if (isFull()) {
            this.arr = Arrays.copyOf(arr,2*arr.length);
        }
        this.arr[useSize] = date;
        useSize++;
    }

    //给一个下标添加元素(插入排序)
    public void add(int pos,int date) {
        //判断下标是否合法
        isLegal(pos);
        if (isFull()) {
            this.arr = Arrays.copyOf(arr,2*arr.length);
        }
        for (int i = this.useSize-1; i >0 ; i--) {
            this.arr[i+1] = arr[i];
        }
        this.arr[pos] = date;
        this.useSize++;
    }

    //判断顺序表是否满了
    public boolean isFull() {
        return this.useSize == arr.length;
    }
    //判断是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if (toFind == arr[i]) {
                return true;
            }
        }
        return false;
    }

    //获取某下标的值
    public int getNum(int pos) {
        isLegal2(pos);
        return this.arr[pos];
    }

    //删除第一次出现的元素
    public boolean delFirst(int date) {
        int flag = indexOf(date);
        if (flag == -1) {
            return false;
        }
        for (int i = flag; i < useSize; i++) {
            this.arr[flag] = this.arr[flag+1];
        }
        this.useSize--;
        this.arr[useSize] = 0;
        return true;
    }
    //修改元素
    public void set(int pos,int date) {
        isLegal2(pos);
        this.arr[pos] = date;
    }
    //判断是否合法
    public void isLegal2(int pos) {
        if (pos <0 || pos>=this.useSize) {
            throw new IndexOutOfException("输入的下标不合法");
        }
    }
    //查找某个元素的位置(下标)
    public int indexOf(int toFind) {
        for (int i = 0; i < this.useSize; i++) {
            if (toFind == arr[i]) {
                return i;
            }
        }
        return -1;
    }
    //获取顺序表长度
    public int size() {
        return this.useSize;
    }

    //获取某下标元素
    public int get(int pos) {

        return 0;
    }

    //判断下标合法性
    public void isLegal(int pos) {
        if (pos <0 || pos>this.useSize) {
            throw new IndexOutOfException("输入的下标不合法");
        }
    }
    //清空顺序表
    public void clearArrayList() {
        for (int i = 0; i < this.useSize; i++) {
            this.arr[i] = 0;
        }
        this.useSize = 0;
    }
}
